package com.hqu.environment.controller.app;

import com.hqu.environment.annotation.IgnoreAuth;
import com.hqu.environment.annotation.LoginUser;
import com.hqu.environment.dao.UserDao;
import com.hqu.environment.dao.VerificationCodeDao;
import com.hqu.environment.dto.UserDTO;
import com.hqu.environment.enums.BooleanEnum;
import com.hqu.environment.utils.ExceptionCode;
import com.hqu.environment.utils.JwtHelper;
import com.hqu.environment.utils.Result;
import com.hqu.environment.utils.Util;
import com.hqu.environment.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @Description: 用户Controller
 * @Date: 2019/04/19 14:47
 * @Author: 邓勇嘉
 **/
@Controller
@RequestMapping("app/user")
public class UserController {
    @Autowired
    VerificationCodeDao verificationCodeDao;
    @Autowired
    UserDao userDao;

    ////获取验证码
    //@IgnoreAuth
    //@RequestMapping("/getVerificationCode")
    //@ResponseBody
    //public Result getVerificationCode(VerificationCodeDTO verificationCodeDTO) {
    //    //判断手机号是否被注册
    //    if(userDao.selectCountByPhone(verificationCodeDTO.getPhone())>0)
    //        return Result.fail(ExceptionCode.Phone_Exist);
    //    //将之前的验证码失效
    //    VerificationCodeDTO v = new VerificationCodeDTO();
    //    v.setPhone(verificationCodeDTO.getPhone());
    //    v.setIsUse(0);
    //    verificationCodeDao.updateByPhoneSelective(v);
    //
    //    //生成新的验证码
    //    String vcode = Util.createVerificationCode();
    //    SmsUtil.execute(verificationCodeDTO.getPhone(),vcode);
    //    verificationCodeDTO.setVerificationCode(vcode);
    //    verificationCodeDao.insertSelective(verificationCodeDTO);
    //    return Result.success().add("vcode", vcode);
    //}

    //登录
    @IgnoreAuth
    @RequestMapping("/login")
    @ResponseBody
    public Result login(UserVO userVO) {
        if(userDao.selectCountByEmpno(userVO.getLoginId())==0&&userDao.selectCountByPhone(userVO.getLoginId())==0)
            return Result.fail(ExceptionCode.USER_NOT_FOUND);

        UserDTO userDTO=userDao.selectPasswordByLoginId(userVO.getLoginId());
        //判断管理员是否同意注册
        if(userDTO.getIsAgree()== BooleanEnum.NO.getIndex())
            return Result.fail(ExceptionCode.USER_NOT_AGREE);

        //判断用户是否被冻结
        if(userDTO.getIsFreeze()==BooleanEnum.YES.getIndex())
            return Result.fail(ExceptionCode.USER_FROZEN);

        //判断密码是否正确
        if(!userDTO.getPassword().equals(userVO.getPassword()))
            return Result.fail(ExceptionCode.PASSWORD_ERROR);

        //生成token
        String token = JwtHelper.genToken(userDTO.getId());
        return Result.success().add("token",token);
    }

    //注册
    @IgnoreAuth
    @RequestMapping("/register")
    @ResponseBody
    public Result register(UserVO userVO){
        if(userDao.selectCountByEmpno(userVO.getEmpno())>0)
            return Result.fail(ExceptionCode.Empno_Exist);
        if(userDao.selectCountByEmail(userVO.getEmail())>0)
            return Result.fail(ExceptionCode.Email_Exist);

        //注册处理
        String filename = Util.uploadFile(userVO.getImage());
        userVO.setHeadUrl(filename);
        userVO.setNickname(userVO.getName());
        UserDTO userDTO =new UserDTO();
        BeanUtils.copyProperties(userVO, userDTO);
        userDao.insertSelective(userDTO);
        return Result.success();

    }


    @RequestMapping("/getUserInfo")
    @ResponseBody
    public Result getUserInfo(@LoginUser Integer userId){
        UserDTO user = userDao.selectByPrimaryKey(userId);
        return Result.success().add("user",user);
    }


    @RequestMapping("/alterUserInfo")
    @ResponseBody
    public Result alterUserInfo(@LoginUser Integer userId,UserVO userVO){
        userVO.setId(userId);
        if(userVO.getImage()!=null){
            String filename = Util.uploadFile(userVO.getImage());
            userVO.setAvatarUrl(filename);
        }
        UserDTO userDTO =new UserDTO();
        BeanUtils.copyProperties(userVO, userDTO);
        userDao.updateByPrimaryKeySelective(userDTO);
        return Result.success();
    }


    @RequestMapping("/test")
    @ResponseBody
    public Result test(@LoginUser Integer u){
        return Result.success();
    }

}
